﻿Imports ADOConnection
Imports System
Imports System.IO

Public Class frmLogin
    Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK.Click
        If txtUserName.Text = "" Then
            ShowMessages("Tên đăng nhập chưa có.")
            Exit Sub
        End If

        g_UserName = Me.txtUserName.Text
        g_Password = Me.txtPassword.Text
        Dim objConnection As IADOConnection


        strLoginName_SA = CUsers.GetGiaTriThanhPhan(myConnStr, "User ID")
        strPassword_SA = CUsers.GetGiaTriThanhPhan(myConnStr, "Password")

        g_objConnFactory = New CConnFactory(myConnStr, strLoginName_SA, strPassword_SA)
        'g_objConnFactory = New CConnFactory(myConnStr.Substring(myConnStr.IndexOf(";") + 1), strLoginName_SA, strPassword_SA)
        objConnection = g_objConnFactory.GetConnection

        Dim msg As String = "Quyền '" + strLoginName_SA + "' không kết nối được."
        If (IsNothing(objConnection) = True) Then
            LoginOK = False
            ShowMessages(msg)
            Exit Sub
        Else
            If (CUsers.DuocPhepChay(g_UserName, g_Password, g_User) = False) Then
                LoginOK = False
                If MessageBox.Show("Đăng nhập không được.Đăng nhập lại?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1) = Windows.Forms.DialogResult.Yes Then
                    Me.txtUserName.Text = ""
                    Me.txtPassword.Text = ""
                End If
            Else
                LoginOK = True
                'InitSQLDMO()
                'g_User = CUsers.GetUserByName(g_UserName)

                'lay ra danh sach cac quyen cua nguoi dung do tren form main
                'truoc tien la xoa het cac quyen cua nguoi su dung truoc
                dsQuyenTrenForm.Clear()

                'gan cau lenh ket noi sao cho chi dung cho SQLServer
                XL_DULIEU.GanCauLenhKetNoi(myConnStr.Substring(myConnStr.IndexOf(";") + 1))
                'thay the chuoi ket noi bang lenh user name va password hien tai
                XL_DULIEU.GanCauLenhKetNoi(strLoginName_SA, strPassword_SA)
                Dim bangDSQuyen As New DataTable()
                Dim cauLenhLayDuLieu = "select mn.VBMenuName from tblUser nsd, tblNHOM n, tblMENU mn, tblNHOM_MENU n_mn where nsd.loginname ='" & g_UserName & "'"
                cauLenhLayDuLieu &= " and nsd.ban_id =n.nhom_id and n.nhom_id = n_mn.nhom_id and n_mn.menu_id = mn.menu_id"
                XL_DULIEU.LayDuLieuRaBang(cauLenhLayDuLieu, bangDSQuyen)
                For Each dong As DataRow In bangDSQuyen.Rows
                    dsQuyenTrenForm.Add(dong("VBMenuName"))
                Next
                objConnection.Close()
                Me.Close()
            End If
        End If
    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel.Click
        Me.Close()
    End Sub

    Private Sub frmLogin_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        myConnStr = GetCnnStrFromFile(Application.StartupPath & "\DBConnect.udl")
        lblCSDL.Text = CUsers.GetGiaTriThanhPhan(myConnStr, "Initial Catalog")
        lblServer.Text = CUsers.GetGiaTriThanhPhan(myConnStr, "Data Source")
    End Sub
End Class
